﻿using NUnit.Framework;
using Rhino.Mocks;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Bus.Subscriptions;
using SimpleServiceBus.Messages.Subscriptions;
using SimpleServiceBus.Tests.Stubs;

namespace SimpleServiceBus.Tests.Bus.Subscriptions
{
    [TestFixture]
    public class SubscriptionRequestHandlerTestFixture
    {
        private static IMessageContext CreateContext(ISubscriptionManager subManager)
        {
            var context = new ServiceLocatorOnlyMessageContext();
            context.RegisterService<ISubscriptionManager>(subManager);
            return context;
        }

        [Test]
        public void Handle_Subscribe_To_All_Request_Delegates_To_Manager()
        {
            var subMgr = MockRepository.GenerateStub<ISubscriptionManager>();
            IMessageContext context = CreateContext(subMgr);
            var message = new SubscribeToAllRequest {SubscriberAddress = "itIsI"};

            var test = new SubscriptionRequestHandler();

            test.Handle(message, context);

            subMgr.AssertWasCalled(action => action.SubscribeToAll(message.SubscriberAddress));
        }

        [Test]
        public void Handle_Subscribe_To_Message_Delegates_To_Manager()
        {
            var subMgr = MockRepository.GenerateStub<ISubscriptionManager>();
            IMessageContext context = CreateContext(subMgr);
            var message = new SubscribeToMessageRequest {MessageType = "System.String", SubscriberAddress = "itIsI"};

            var test = new SubscriptionRequestHandler();

            test.Handle(message, context);

            subMgr.AssertWasCalled(action => action.SubscribeToMessage(typeof (string), message.SubscriberAddress));
        }

        [Test]
        public void Handle_Unsubscribe_From_All_Delegates_To_Manager()
        {
            var subMgr = MockRepository.GenerateStub<ISubscriptionManager>();
            IMessageContext context = CreateContext(subMgr);
            var message = new UnsubscribeFromAllRequest {SubscriberAddress = "itIsI"};

            var test = new SubscriptionRequestHandler();

            test.Handle(message, context);

            subMgr.AssertWasCalled(action => action.UnsubscribeFromAll(message.SubscriberAddress));
        }

        [Test]
        public void Handle_Unsubscribe_From_Message_Delegates_To_Manager()
        {
            var subMgr = MockRepository.GenerateStub<ISubscriptionManager>();
            IMessageContext context = CreateContext(subMgr);
            var message = new UnsubscribeFromMessageRequest {MessageType = "System.String", SubscriberAddress = "itIsI"};

            var test = new SubscriptionRequestHandler();

            test.Handle(message, context);

            subMgr.AssertWasCalled(action => action.UnsubscribeFromMessage(typeof (string), message.SubscriberAddress));
        }
    }
}